package com.nhim.cms.admin.client.template.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.mastergaurav.codemirror.client.CodeEditor;
import com.mastergaurav.codemirror.client.CodeEditorToolbar;
import com.mastergaurav.codemirror.client.CodeMirrorConfig;
import com.mastergaurav.codemirror.client.TabMode;
import com.nhim.cms.admin.client.rpc.TemplateRPCService;
import com.nhim.cms.admin.client.rpc.TemplateRPCServiceAsync;
import com.nhim.cms.admin.client.template.ui.TemplateResources.Listener;
import com.nhim.cms.admin.shared.TemplateResourceProxy;

public class TemplateEdit extends Composite {

	private static TemplateEditUiBinder uiBinder = GWT
			.create(TemplateEditUiBinder.class);

	interface TemplateEditUiBinder extends UiBinder<Widget, TemplateEdit> {
	}

	public TemplateEdit() {
		initWidget(uiBinder.createAndBindUi(this));
		InitEditor();
	}

	private final TemplateRPCServiceAsync service = GWT
			.create(TemplateRPCService.class);
	@UiField
	FlowPanel templateEditor;
	@UiField
	ListBox lbFiles;
	@UiField
	ListBox lbElementTemplates;
	@UiField
	ListBox lbElementDefinitions;
	@UiField
	ListBox lbCustomWidgets;
	@UiField
	ListBox lbSystemInformation;
	private String selectedFile = "";
	private Integer previousSelectedIndex = 0;
	Integer templateId;
	CodeEditor editor;

	private void InitEditor() {
		CodeMirrorConfig config = new CodeMirrorConfig();
		config.setParserFile("parsexml.js", "parsejavascript.js",
				"parsecss.js", "parsehtmlmixed.js");
		config.setHeight(452, Unit.PX);
		config.setWidth(710, Unit.PX);
		config.setStylesheet(GWT.getModuleBaseURL() + "css/xmlcolors.css",
				GWT.getModuleBaseURL() + "css/csscolors.css",
				GWT.getModuleBaseURL() + "css/jscolors.css",
				GWT.getModuleBaseURL() + "css/sparqlcolors.css");
		config.setTabmode(TabMode.SHIFT);
		config.setShowLineNumbers(true);
		editor = new CodeEditor(config);
		CodeEditorToolbar tools = editor.getToolbar();
		tools.removeFromParent();
		templateEditor.add(editor);
	}

	public void Display(Integer id, TemplateResources resources) {
		templateId = id;
		bindFilesToEdit();
		bindElementTemplates();
		bindElementDefinitions();
		bindCustomWidgets();
		bindSystemInfomation();
		resources.setListener(new Listener() {

			@Override
			public void onResourceChange() {
				bindFilesToEdit();

			}
		});
		lbFiles.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent arg0) {
				if (!lbFiles.getValue(lbFiles.getSelectedIndex())
						.equalsIgnoreCase("--Select--")) {
					selectedFile = lbFiles.getValue(lbFiles.getSelectedIndex());
					previousSelectedIndex = lbFiles.getSelectedIndex();
					service.retrieveContent(templateId,
							lbFiles.getValue(lbFiles.getSelectedIndex()),
							new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable error) {

									Window.alert(error.getMessage());
								}

								@Override
								public void onSuccess(String content) {
									if (editor != null)
										editor.setValue(content);

								}
							});
				} else {
					lbFiles.setSelectedIndex(previousSelectedIndex);
				}

			}
		});
	}

	private void bindFilesToEdit() {

		service.findEditableResoures(templateId,
				new AsyncCallback<List<TemplateResourceProxy>>() {

					@Override
					public void onFailure(Throwable error) {
						Window.alert(error.getMessage());

					}

					@Override
					public void onSuccess(List<TemplateResourceProxy> result) {
						lbFiles.clear();
						lbFiles.addItem("--Select--");
						boolean resetEditor = true;
						for (TemplateResourceProxy proxy : result) {
							if (proxy.getFileName().equals(selectedFile)) {
								previousSelectedIndex = lbFiles.getItemCount();
								resetEditor = false;
							}
							lbFiles.addItem(proxy.getFileName());
						}
						if (resetEditor) {
							selectedFile = "";
							editor.setValue("");
						} else
							lbFiles.setSelectedIndex(previousSelectedIndex);

					}
				});
	}
	public void save()
	{
		String fileName = lbFiles.getValue(lbFiles.getSelectedIndex());
		if(!fileName.equalsIgnoreCase("--Select--"))
		{
			service.saveFile(templateId, editor.getValue(), fileName, new AsyncCallback<Boolean>(){

				@Override
				public void onFailure(Throwable error) {
					Window.alert(error.getMessage());
				}

				@Override
				public void onSuccess(Boolean result) {
					if(!result)
						Window.alert("A error was occured while saving template file");
					
				}});
		}
	}

	private void bindElementTemplates() {
		lbElementTemplates.addItem("--Select--");
		lbElementTemplates.addItem("Not Implemented");
	}

	private void bindElementDefinitions() {
		lbElementDefinitions.addItem("--Select--");
		lbElementDefinitions.addItem("Not Implemented");
	}

	private void bindCustomWidgets() {
		lbCustomWidgets.addItem("--Select--");
		lbCustomWidgets.addItem("Not Implemented");
	}

	private void bindSystemInfomation() {
		lbSystemInformation.addItem("--Select--");
		lbSystemInformation.addItem("Not Implemented");

	}

}
